package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.grandchild.GrandChildChampion;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class GrandChildChanpionDaoImpl extends TempDataObjectDao<GrandChildChampion> {

    public static String getGrandChildChampionSql = "select * from `t_c_grand_child_champion`";

    public Map<Long, Map<Long, GrandChildChampion>> getGrandChildChampionMap() {
        Map<Long, Map<Long, GrandChildChampion>> result = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getGrandChildChampionSql);
            rs = call.executeQuery();
            while (rs.next()) {
                GrandChildChampion parse = parse(rs);
                if (!result.containsKey(parse.getServerId())) {
                    result.put(parse.getServerId(), new ConcurrentHashMap<>());
                }
                result.get(parse.getServerId()).put(parse.getUserId(), parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String updateGrandChildChampionSql = "update `t_c_grand_child_champion` set count=?,updateTime=? where serverId=? and userId=?";

    @Override
    protected boolean updateData(GrandChildChampion grandChildChampion) {
        grandChildChampion.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, grandChildChampion.getCount()));
        para.put(i++, new DbParameter(Types.BIGINT, grandChildChampion.getUpdateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, grandChildChampion.getServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, grandChildChampion.getUserId()));
        if (getDb().executeNoneQuery(updateGrandChildChampionSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertGrandChildChampionSql = "insert into `t_c_grand_child_champion`(serverId,userId,count,updateTime) values (?,?,?,?)";

    @Override
    protected boolean addData(GrandChildChampion grandChildChampion) {
        grandChildChampion.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, grandChildChampion.getServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, grandChildChampion.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, grandChildChampion.getCount()));
        para.put(i++, new DbParameter(Types.BIGINT, grandChildChampion.getUpdateTime()));
        if (getDb().executeNoneQuery(insertGrandChildChampionSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected GrandChildChampion parseData(ResultSet rs) throws SQLException {
        GrandChildChampion grandChildChampion = new GrandChildChampion();
        grandChildChampion.setServerId(rs.getLong("serverId"));
        grandChildChampion.setUserId(rs.getLong("userId"));
        grandChildChampion.setCount(rs.getLong("count"));
        grandChildChampion.setUpdateTime(rs.getLong("updateTime"));
        return grandChildChampion;
    }


}
